package com.fourpigeon.domain;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.annotation.TableName;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fourpigeon.utils.COSUtils;
import com.fourpigeon.utils.LocalDateTimeSerializerUtils;
import com.qcloud.cos.http.HttpMethodName;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.format.annotation.DateTimeFormat;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
//
@Data
@NoArgsConstructor
@AllArgsConstructor
@TableName("tb_blog_practice")
public class BlogPractice{
    /**
     * 主键
     */
    private Long id;
    /**
     * 用户id
     */
    private Long userId;
    /**
     * 用户头像
     */
    private String icon;
    /**
     * 用户名称
     */
    private String userName;
    /**
     * 培教类型：0：找陪练，1：找教练
     */
    private Short type;
    /**
     * 需求
     */
    private String demand;
    /**
     * 时间
     */
    private String time;
    /**
     * 地点
     */
    private String location;
    /**
     * 价格
     */
    private String price;
    /**
     * 人数
     */
    private String people;
    /**
     * 补充内容
     */
    private String content;
    /**
     * 联系方式
     */
    private String contact;
    /**
     * 状态码：0正常，1：被举报，2：禁止查看
     */
    private Integer status = 0;
    /**
     * 创建时间
     */
    @JsonDeserialize(using = LocalDateTimeSerializerUtils.LocalDateTimeDeserializer.class)
    @JsonSerialize(using = LocalDateTimeSerializerUtils.LocalDateTimeSerializer.class)
    private LocalDateTime createTime;
    /**
     * 更新时间
     */
    @JsonDeserialize(using = LocalDateTimeSerializerUtils.LocalDateTimeDeserializer.class)
    @JsonSerialize(using = LocalDateTimeSerializerUtils.LocalDateTimeSerializer.class)
    private LocalDateTime updateTime;

    /**
     * 头像url
     */
    private String iconUrl = "";

    public void set(Long id,User user){
        this.id = id;
        this.userId = user.getId();
        this.icon = user.getIcon();
        this.userName = user.getName();
        this.createTime = LocalDateTime.now();
        this.updateTime = LocalDateTime.now();
    }

    public BlogPractice matchAndUpdate(BlogPracticeDTO blogPracticeDTO){
        //结果列表，用于返回不同的元素的key
        List<Object> resultList = new ArrayList<>();
        Map<String, Object> oriMap = BeanUtil.beanToMap(this);
        Map<String, Object> newMap = BeanUtil.beanToMap(blogPracticeDTO);
        for (String key : newMap.keySet()) {
            if(key.equals("iconUrl")) continue;
            if (!newMap.get(key).equals(oriMap.get(key))){
                oriMap.remove(key);
                oriMap.put(key,newMap.get(key));
                resultList.add(key);
            }
        }
        //无不同元素
        if (resultList.isEmpty()) {
            return null;
        }
        BlogPractice temp = BeanUtil.mapToBean(oriMap, BlogPractice.class, false);
        return temp;
    }

    public BlogPractice responseType(){
        this.status = null;
        this.createTime = null;
        this.updateTime = null;
        return this;
    }

    public void handlerImgs() {
        this.iconUrl = COSUtils.getSign(this.icon,HttpMethodName.GET);
    }
}
